package com.jm.web.builder;

import java.io.IOException;
import java.util.Iterator;

import com.jm.utils.PageJsGear;
import com.jm.utils.StringBuilderEx;
import com.jm.utils.StringHelper;
public class FormBuilder extends BaseBuilder{
	@Override
	public void RenderStart() {
		StringBuilderEx sb = new StringBuilderEx();
		//註冊loading
		if(this.getBaseTag().getPage().isLoading() && !this.getBaseTag().getPage().isExport()){
			sb.Append("<div id=\"ol_loading_mask\" class=\"ol_loading_mask\" style=\"width:90%%;\"></div>");
			sb.Append("<div id=\"ol_loading\" class=\"ol_loading\" style=\"font-size:12px;padding-top:70px;width:90%%;\">[<span id=\"loadingtext\" style=\"font-weight:bold;color:blue;\">頁面</span>] %1$s...</div>",this.getBaseTag().getPage().getUtils().i18n("加載中"));
			sb.Append("<script>");
			
			sb.Append(PageJsGear.getLogBebinJs(this.getBaseTag().getPage(),"loading"));
			sb.Append("$P.loading=new ol.loading();$P.loading.show();");
			sb.Append(PageJsGear.getLogEndJs(this.getBaseTag().getPage(),"loading"));
			
			sb.Append("</script>");
		}
		
		//form
		sb.Append("<form defaultbutton=\"search\"");
		sb.Append(" method=\"post\" ");
		sb.Append(" id=\"%1$s\"",this.getBaseTag().GetExtValue("id","form1"));
		sb.Append(" name=\"%1$s\"",this.getBaseTag().GetExtValue("name","form1"));
		if(!this.getBaseTag().getPage().isformNoAction())
			sb.Append(" action=\"%1$s\"",this.getBaseTag().getPage().getPageName());
		
		if(this.getBaseTag().IsContainsKey("enctype")){
			sb.Append(" enctype=\"%1$s\" ",this.getBaseTag().GetExtValue("enctype",""));
		}
		
		if(this.getBaseTag().IsContainsKey("style")){
			sb.Append(" style=\"%1$s\"",this.getBaseTag().GetExtValue("style",""));
		}
		
		if(this.getBaseTag().IsContainsKey("class")){
			sb.Append(" class=\"%1$s\"",this.getBaseTag().GetExtValue("class",""));
		}
		
		if(this.getBaseTag().IsContainsKey("width")){
			sb.Append(" width=\"%1$s\"",this.getBaseTag().GetExtValue("width",""));
		}
		
		if(this.getBaseTag().IsContainsKey("height")){
			sb.Append(" height=\"%1$s\"",this.getBaseTag().GetExtValue("height",""));
		}
		if(this.getBaseTag().IsContainsKey("onclick")){
			sb.Append(" onclick=\"%1$s\"",this.getBaseTag().GetExtValue("onclick",""));
		}
		
		if(this.getBaseTag().IsContainsKey("onchange")){
			sb.Append(" onchange=\"%1$s\"",this.getBaseTag().GetExtValue("onchange",""));
		}
		
		if(this.getBaseTag().IsContainsKey("onblur")){
			sb.Append(" onblur=\"%1$s\"",this.getBaseTag().GetExtValue("onblur",""));
		}
		
		if(this.getBaseTag().IsContainsKey("onfocus")){
			sb.Append(" onfocus=\"%1$s\"",this.getBaseTag().GetExtValue("onfocus",""));
		}
		
		if(this.getBaseTag().IsContainsKey("onkeydown")){
			sb.Append(" onkeydown=\"%1$s\"",this.getBaseTag().GetExtValue("onkeydown",""));
		}
		
		sb.Append(">");
		sb.Append(this.getBaseTag().getPage().outputHiddenItem());
		try {
			this.getOut().write(sb.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	public void RenderEnd() {
		try {
			this.getOut().write("</form>");
			
			if(this.getBaseTag().getPage() != null && this.getBaseTag().getPage().getItems() != null){
				int i = 0;
				StringBuilderEx sb = new StringBuilderEx();
				sb.Append(PageJsGear.getLogBebinJs(this.getBaseTag().getPage(),"itemsSet"));
				for (Iterator it = this.getBaseTag().getPage().getItems().keySet().iterator();it.hasNext();){
					String key = (String)it.next();
					Object obj = this.getBaseTag().getPage().getItems().get(key);
	//				if(obj instanceof Input || obj instanceof Select){
						sb.Append("$P.form.items[%1$s]=\"%2$s\";",i,key);
						i++;
	//				}
				}
				sb.Append(PageJsGear.getLogEndJs(this.getBaseTag().getPage(),"itemsSet"));
				this.getOut().write(StringHelper.Format("<script > %1$s </script>",sb.toString()));
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
